package com.shop.cereshop.admin.param.wallet;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.shop.cereshop.commons.config.RsaProperties;
import com.shop.cereshop.commons.utils.EmptyUtils;
import com.shop.cereshop.commons.utils.RsaUtils;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.NotBlank;

@Data
@ApiModel(value = "AdminModifyPayPasswordParam", description = "管理员修改用户支付密码请求参数")
public class AdminModifyPayPasswordParam {

    @ApiModelProperty(value = "用户ID", required = true)
    @NotNull(message = "buyerUserId不能为空")
    private Long buyerUserId;

    @ApiModelProperty(value = "加密后的支付密码", required = true, notes = "使用RSA公钥加密的6位数字支付密码")
    @NotBlank(message = "支付密码不能为空")
    private String encryptedPayPassword;

    @ApiModelProperty(value = "加密后的确认支付密码", required = true, notes = "使用RSA公钥加密的6位数字确认密码")
    @NotBlank(message = "确认支付密码不能为空")
    private String encryptedConfirmPayPassword;

    @JsonIgnore
    public String decryptPayPassword() {
        if (EmptyUtils.isEmpty(encryptedPayPassword)) {
            return null;
        }
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, encryptedPayPassword);
        } catch (Exception e) {
            throw new RuntimeException("支付密码解密失败", e);
        }
    }

    @JsonIgnore
    public String decryptConfirmPayPassword() {
        if (EmptyUtils.isEmpty(encryptedConfirmPayPassword)) {
            return null;
        }
        try {
            return RsaUtils.decryptByPrivateKey(RsaProperties.privateKey, encryptedConfirmPayPassword);
        } catch (Exception e) {
            throw new RuntimeException("确认支付密码解密失败", e);
        }
    }

    @JsonIgnore
    public boolean isPayPasswordValidFormat() {
        String password = decryptPayPassword();
        return password != null && password.matches("^\\d{6}$");
    }

    @JsonIgnore
    public boolean isConfirmPayPasswordValidFormat() {
        String password = decryptConfirmPayPassword();
        return password != null && password.matches("^\\d{6}$");
    }

    @JsonIgnore
    public boolean isPasswordMatch() {
        String password = decryptPayPassword();
        String confirmPassword = decryptConfirmPayPassword();
        return password != null && confirmPassword != null && password.equals(confirmPassword);
    }
}


